package com.gao.frame.study.shop.config.client;

import sun.management.resources.agent;

import java.util.Properties;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.ThreadFactory;
import java.util.concurrent.TimeUnit;

/**
 * <p></p>
 *
 * @author GaoShuangWen
 * @since 2019-09-27 13:58
 */
public class ConfigClient {

    final ScheduledExecutorService executor;
    final ScheduledExecutorService executorService;

    public ConfigClient() {
        // 这个线程池 执行定时任务的 executor
        executor = Executors.newScheduledThreadPool(1, r -> {
            Thread t = new Thread(r);
            t.setName("Worker Thread" );
            t.setDaemon(true);
            return t;
        });
        // 第二个线程池是一个普通的线程池，从 ThreadFactory 的名称可以看到这个线程池是做长轮询的
        executorService = Executors.newScheduledThreadPool(Runtime.getRuntime().availableProcessors(), r -> {
            Thread t = new Thread(r);
            t.setName("LongPolling Thread");
            t.setDaemon(true);
            return t;
        });
        // executor 每隔 10ms 就会执行一次 checkConfigInfo() 方法，从方法名上可以知道是每 10 ms 检查一次配置信息
        executor.scheduleWithFixedDelay(() -> {
            try {
                isUpdate();
            } catch (Throwable e) {

            }
        }, 1L, 10L, TimeUnit.MILLISECONDS);
    }

    /**
     * 1.获取配置内容 http
     *
     */
    public String getContext(){
        return null;
    }
    /**
     * 查看是否有更新
     */
    public String[] isUpdate(){
        System.out.println("--------执行操作了哦----------");
        return null;
    }
    /**
     * 动态感知
     *   长轮训的操作
     */
    public static void main(String[] args) throws InterruptedException {
        ConfigClient configClient=new ConfigClient();
        Thread.sleep(3000000);
    }


}
